package com.derbysoft.lowcostadapter.dswitch3.translate.impl;

import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailCriteriaDTO;
import com.derbysoft.dswitch.dto.hotel.common.StayDateRangeDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailRequest;
import com.derbysoft.lowcostadapter.commons.dto.*;
import com.derbysoft.lowcostadapter.commons.utils.DateTimeUtils;
import com.derbysoft.lowcostadapter.dswitch3.translate.Translator;
import org.springframework.stereotype.Component;

/**
 * Created by: jason
 * Date: 2012-06-18
 */
@Component("ds3HotelAvailRQTranslator")
public class DSwitch3AvailRQTranslator extends AbstractDSwitch3Translator
        implements Translator<HotelAvailRequest, Search, Void> {
    @Override
    public Search translate(HotelAvailRequest request, Void aVoid) {
        Search search = new Search();
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setLoginDetails(createLoginDetails(request.getHeader().getSource()));
        searchRequest.setSearchDetails(createSearchDetails(request));
        search.setSearchRequest(searchRequest);
        return search;
    }

    private SearchDetails createSearchDetails(HotelAvailRequest request) {
        SearchDetails searchDetails = new SearchDetails();

        HotelAvailCriteriaDTO availCriteria = request.getHotelAvailRQ().getAvailCriteria();
        searchDetails.setPropertyID(loadHotel(availCriteria.getHotelCodesList().get(0)).getPropertyID());

        StayDateRangeDTO stayDateRangeDTO = availCriteria.getStayDateRange();
        searchDetails.setArrivalDate(stayDateRangeDTO.getCheckin());
        searchDetails.setDuration(
                DateTimeUtils.getIntervalDays(
                        stayDateRangeDTO.getCheckin(),
                        stayDateRangeDTO.getCheckout()));

        searchDetails.setRoomRequests(createRoomRequests(availCriteria));

        return searchDetails;
    }

    private ArrayOfRoomRequest createRoomRequests(HotelAvailCriteriaDTO hotelAvailCriteriaDTO) {
        ArrayOfRoomRequest arrayOfRoomRequest = new ArrayOfRoomRequest();
        for (int roomIndex = 0; roomIndex < hotelAvailCriteriaDTO.getRoomStayCandidate().getNumberOfUnits(); roomIndex++) {
            arrayOfRoomRequest.getRoomRequest().add(createRoomRequest(hotelAvailCriteriaDTO));
        }
        return arrayOfRoomRequest;
    }

    private RoomRequest createRoomRequest(HotelAvailCriteriaDTO hotelAvailCriteriaDTO) {
        RoomRequest roomRequest = new RoomRequest();

        roomRequest.setAdults(hotelAvailCriteriaDTO.getRoomStayCandidate().getAdultCount());

        roomRequest.setChildren(hotelAvailCriteriaDTO.getRoomStayCandidate().getChildCount());

        if (roomRequest.getChildren() > 0) {
            ArrayOfChildAge arrayOfChildAge = new ArrayOfChildAge();
            for (int childIndex = 0; childIndex < roomRequest.getChildren(); childIndex++) {
                arrayOfChildAge.getChildAge().add(createChildAge(DEFAULT_CHILD_AGE));
            }
            roomRequest.setChildAges(arrayOfChildAge);
        }

        return roomRequest;
    }

    private ChildAge createChildAge(int age) {
        ChildAge childAge = new ChildAge();
        childAge.setAge(age);
        return childAge;
    }
}
